package com.glodon.paas.job.model;

import java.util.Date;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.glodon.paas.job.util.DateFormatUtil;

@Entity
@Table(name = "agent_status")
public class AgentStatus {
    private static final String UPDATE_TIME = "updateTime";
    private static final String ADDRESS = "address";
    @Id
    private String address;
    private Date updateTime;

    public AgentStatus() {
    }

    public AgentStatus(String address, Date updateTime) {
        this.address = address;
        this.updateTime = updateTime;
    }

    public static AgentStatus createAgentStatus(Message message) throws JMSException {
        String address = message.getStringProperty(ADDRESS);
        Date updateTime = DateFormatUtil.parse(message.getStringProperty(UPDATE_TIME));
        return new AgentStatus(address, updateTime);
    }

    public void toMessage(Message message) throws JMSException {
        message.setStringProperty(ADDRESS, address);
        message.setStringProperty(UPDATE_TIME, DateFormatUtil.format(updateTime));
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getUpdateTimeStr() {
        return DateFormatUtil.format(updateTime);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
